﻿using System;
using System.Drawing;
using System.IO;
using LockImage.Encryption;
using LockImage.Utils;

namespace LockImage.Gui.Dialog
{
    public partial class EnterPasswordDialog
    {
        public void LoadEncryptedImage()
        {
            byte[] imageAsByteArray = ResourceManager.LoadResource(MainApplication.THIS_EXECUATABLE_FULL_PATH);
            Image decryptedImage = null;
            IEncryptionEngine encryption = new RijndaelEncryptionEngine();
            try
            {
                decryptedImage = Image.FromStream(new MemoryStream(encryption.Decrypt(imageAsByteArray, this.passwordTextBox.Text)));
            }
            catch (Exception ex)
            {
                // since no image could created from the decrypted date,
                // the password must be wrong.
                this.wrongPasswordLabel.Visible = true;
                return;
            }
            this.Hide();
            MainApplication.mainForm = new MainForm();
            MainApplication.mainForm.ShowImage(decryptedImage);
            MainApplication.mainForm.Show();
        }

        private void close()
        {
            base.Close();
        }
    }
}